<?php


namespace Staff\Controller;

use Zend\Form\Form;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;

// <-- Add this import
use Staff\Model\Staff;
use Staff\Form\StaffForm;
use Staff\Form\StaffQueryForm;

class StaffController extends AbstractActionController
{
    protected $staffTable;

    public function getStaffTable()
    {
        if (!$this->staffTable) {
            $sm = $this->getServiceLocator();
            $this->staffTable = $sm->get('Staff\Model\StaffTable');
        }
        return $this->staffTable;
    }

    public function indexAction()
    {
        $form = new StaffQueryForm();
        $column = $this->params()->fromQuery('column', null);
        $parame = $this->params()->fromQuery('parame', null);
        $staffs = $this->getStaffTable()->findStaff($column, $parame);
        return new ViewModel(array(
            'form' => $form,
            'column' => $column,
            'parame' => $parame,
            'staffs' => $staffs
        ));
    }

    // Add content to this method
    public function addAction()
    {
        $form = new StaffForm();
        $form->get('submit')->setAttribute('value', '确定')->setAttribute('class', 'btn btn-primary');
        if ($this->getRequest()->isPost()) {
            $staff = new Staff();
            //$form->bind($staff);
            $form->setInputFilter($staff->getInputFilter());
            $form->setData($this->getRequest()->getPost());
            if ($form->isValid()) {
                $staff->exchangeArray($form->getData());
                $this->getStaffTable()->saveStaff($staff);
                // Redirect to list of staffs
                return $this->redirect()->toRoute('staff');
            }
        }
        return array('form' => $form);
    }

    // Add content to this method
    public function editAction()
    {
        $staff_id = $this->params()->fromRoute('id', null);
        if (is_null($staff_id)) return $this->redirect()->toRoute('staff', array('action' => 'add'));
        // if it cannot be found, in which case go to the index page
        try {
            $staff = $this->getStaffTable()->getStaffByStaffId($staff_id);
//            var_dump($staff);
        } catch (\Exception $ex) {
            return $this->redirect()->toRoute('staff', array('action' => 'index'));
        }
        $form = new StaffForm();
        $form->bind($staff);
        $form->get('submit')->setAttribute('value', '确定')->setAttribute('class', 'btn btn-primary');
        if ($this->getRequest()->isPost()) {
            $form->setInputFilter($staff->getInputFilter());
            $form->setData($this->getRequest()->getPost());
            if ($form->isValid()) {
                $this->getStaffTable()->setStaff($staff);
                // Redirect to list of staffs
                return $this->redirect()->toRoute('staff');
            }
        }
        return array(
            'staff_id' => $staff_id,
            'form' => $form
        );
    }

    // Add content to the following method
    public function deleteAction()
    {
        $staff_id = $this->params()->fromRoute('id', null);
        if (is_null($staff_id)) return $this->redirect()->toRoute('staff', array('action' => 'delete'));
        if ($this->getRequest()->isPost()) {
            if ($this->getRequest()->getPost('del', 'No') == 'Yes') {
                $staff_id = $this->getRequest()->getPost('staff_id');
                $this->getStaffTable()->deleteStaff($staff_id);
            }
            return $this->redirect()->toRoute('staff');
        }
        return array(
            'staff_id' => $staff_id,
            'staff' => $this->getStaffTable()->getStaff($staff_id)
        );
    }
}